<template>
  <ContentWrap class="chem-record-wrap">
    <!-- 头部信息表单 -->

    <!-- 表格 -->
    <el-table
      :data="form.table"
      border
      class="chem-table"
      stripe
      :span-method="arraySpanMethod"
    >
      <el-table-column align="center" label="工作场所" prop="workplace">
        <template #default="{ row }">
          <el-input v-model="row.workplace" placeholder="" @change="emitChange" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="检测岗位" prop="detectionPost">
        <template #default="{ row }">
          <el-input v-model="row.detectionPost" placeholder="" @change="emitChange" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="检测项目" prop="item">
        <template #default="{ row }">
          <el-input v-model="row.item" placeholder="" @change="emitChange" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="采样点及时机" prop="samplePoint">
        <template #default="{ row }">
          <el-input v-model="row.samplePoint" placeholder="" @change="emitChange" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="样品编号" prop="sampleNo">
        <template #default="{ row }">
          <el-input v-model="row.sampleNo" placeholder="" @change="emitChange" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="原始结果 C(mg/m³)" prop="result" width="150">
        <template #default="{ row }">
          <el-input v-model="row.result" placeholder="" @change="emitChange" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="代表接触时间T(min)" prop="contactTime" width="150">
        <template #default="{ row }">
          <el-input v-model="row.contactTime" placeholder="" @change="emitChange" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="C15(mg/m³)" prop="c15Result" width="120">
        <template #default="{ row }">
          <el-input v-model="row.c15Result" placeholder="" @change="emitChange" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="CSTE/CPE(mg/m³)" prop="csteCpeResult" width="150">
        <template #default="{ row }">
          <el-input v-model="row.csteCpeResult" placeholder="" @change="emitChange" />
        </template>
      </el-table-column>
    </el-table>
  </ContentWrap>
</template>

<script lang="ts" setup>
import { onMounted, ref, watch } from 'vue'
import ContentWrap from '@/components/ContentWrap/src/ContentWrap.vue'

// 定义数据类型
interface TableRow {
  workplace: string
  detectionPost: string
  item: string
  samplePoint: string
  sampleNo: string
  result: string
  contactTime: string
  c15Result: string
  csteCpeResult: string
  [key: string]: any
}

interface FormData {
  type: string
  table: TableRow[]
}

// 新增type和formData prop
const props = defineProps<{
  formData?: any
}>()

const emit = defineEmits(['change'])

defineOptions({ name: 'CSTSDataProcessing' })

const form = ref<FormData>({
  type: '',
  table: []
})

watch(
  () => props.formData,
  (val) => {
    if (!val) return
    form.value.table = val.cste || []
    form.value.type = val.type || ''
  },
  { immediate: true }
)

const emitChange = () => {
  const payload = JSON.parse(JSON.stringify(form.value))
  emit('change', payload)
}

// 合并单元格方法
const arraySpanMethod = ({ row, column, rowIndex, columnIndex }: any) => {
  if (!form.value.table || form.value.table.length === 0) {
    return [1, 1]
  }

  // 仅对前两列（工作场所、检测岗位）分组合并
  // 第1列：按自身值；第2列：按第1+2列共同键
  if (columnIndex === 0 || columnIndex === 1) {
    const getGroupKey = (r: any) => {
      if (columnIndex === 0) return `${r.workplace}`
      return `${r.workplace}__${r.detectionPost}`
    }

    const currentKey = getGroupKey(form.value.table[rowIndex])

    if (rowIndex > 0) {
      const prevKey = getGroupKey(form.value.table[rowIndex - 1])
      if (prevKey === currentKey) {
        return [0, 0]
      }
    }

    let rowspan = 1
    for (let i = rowIndex + 1; i < form.value.table.length; i++) {
      const nextKey = getGroupKey(form.value.table[i])
      if (nextKey === currentKey) {
        rowspan++
      } else {
        break
      }
    }
    return [rowspan, 1]
  }

  return [1, 1]
}

onMounted(() => {})
</script>

<style lang="scss" scoped>
.chem-title {
  font-size: 26px;
  font-weight: 600;
  color: #222;
  flex-grow: 1;
  text-align: center;
}

.chem-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 0 0 24px 0;
  padding: 16px 0 16px 0;
  border-bottom: 1.5px solid #409eff;
  font-size: 20px;
  font-weight: 600;
  color: #409eff;
  background: none;
  border-radius: 0;
  box-shadow: none;
  letter-spacing: 1px;
}

.chem-form {
  background: none;
  box-shadow: none;
  border-radius: 0;
  margin-bottom: 0;

  :deep(.el-input__inner) {
    border-radius: 0 !important;
    box-shadow: none !important;
  }

  :deep(.el-form-item__label) {
    white-space: nowrap;
  }
}

.chem-btns {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  align-items: center;
  margin-top: 0;
}

.chem-table {
  margin-top: 18px;
  --el-table-border-radius: 0 !important;
  --el-table-header-bg-color: #f7f8fa;
  --el-table-border-color: #e5e6eb;
  box-shadow: none !important;
  border-radius: 0 !important;

  :deep(.el-table__header th) {
    background: #f7f8fa !important;
    font-weight: 600;
    font-size: 14px;
    border-radius: 0 !important;
    padding: 8px 0 !important;

    .cell {
      padding-left: 0px !important;
      padding-right: 0px !important;
    }
  }

  // 设置表格行高
  :deep(.el-table__body td) {
    border-radius: 0 !important;

    font-size: 14px;
    padding: 0 !important;
    // 列和单元格的边距
    .cell {
      padding: 0px 0px;
    }
  }

  // 去掉输入框边框
  :deep(.el-input__wrapper) {
    border-radius: 0 !important;
    box-shadow: none !important;
  }

  // 针对下拉选择框的所有相关元素去掉边框
  :deep(.el-select__wrapper) {
    border: none !important;
    box-shadow: none !important;
  }
}

.input-with-unit {
  position: relative;
}

.unit-text {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: #909399;
  font-size: 14px;
  pointer-events: none;
  z-index: 1;
}
</style>
